package com.qipay.admin.card.service;

import com.qipay.admin.card.vo.CardBindVo;
import com.qipay.baas.model.CardBind;
import com.qipay.card.service.CardBindService;
import com.qipay.security.UserDetailsServiceImpl;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Creator: Yao
 * Date:    2017/10/19
 * For:
 * Other:
 */
@Service
public class CardBindLogicService {

    @Autowired
    CardBindService cardBindService;


    public CardBindVo get(Long cardBindId) {

        // check
        Assert.notNull(cardBindId);

        // init
        CardBindVo cardBindVo = null;

        // query
        CardBind cardBind = cardBindService.get(cardBindId);
        if (null != cardBind) {
            cardBindVo = new CardBindVo();
            BeanUtils.copyProperties(cardBind, cardBindVo);
        }

        return cardBindVo;
    }

    public void save(UserDetailsServiceImpl.LoginUserDetails loginUser, CardBindVo cardBind) {

        // check
        Assert.notNull(loginUser);
        Assert.notNull(cardBind);

        // init
        cardBind.setUserId(Long.valueOf(loginUser.getId()));

        // save
        cardBindService.save(cardBind);
    }

    public List<CardBindVo> page(Integer userId, Integer page, Integer size) {

        // check
        Assert.notNull(userId);
        Assert.notNull(page);
        Assert.notNull(size);

        // init
        List<CardBindVo> voList = null;

        // query
        List<CardBind> list = cardBindService.page(userId, page, size);
        if (CollectionUtils.isNotEmpty(list)) {

            voList = new ArrayList<>();
            for (CardBind p : list) {

                CardBindVo q = new CardBindVo();
                BeanUtils.copyProperties(p, q);
                voList.add(q);
            }
        }

        return voList;
    }

    public long count(Integer userId) {

        // check
        Assert.notNull(userId);

        return cardBindService.count(userId);
    }

    public void delete(Long bindId) {

        // check
        Assert.notNull(bindId);

        cardBindService.delete(bindId);
    }
}
